<!--

    Copyright © 2016-2023 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-toolbar color="primary">
  <h2>{{ dialogTitle | translate }}</h2>
  <span fxFlex></span>
  <div tb-help="templateNotifications"></div>
  <button mat-icon-button
          (click)="cancel()"
          type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
</mat-progress-bar>
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
<div mat-dialog-content>
  <mat-horizontal-stepper linear #notificationTemplateStepper
                          [labelPosition]="(stepperLabelPosition | async)"
                          [orientation]="(stepperOrientation | async)"
                          (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="templateNotificationForm">
      <ng-template matStepLabel>{{ 'notification.basic-settings' | translate }}</ng-template>
      <form [formGroup]="templateNotificationForm" style="padding-bottom: 16px;">
        <mat-form-field class="mat-block">
          <mat-label translate>notification.name</mat-label>
          <input matInput formControlName="name" required>
          <mat-error *ngIf="templateNotificationForm.get('name').hasError('required')">
            {{ 'notification.name-required' | translate }}
          </mat-error>
        </mat-form-field>
        <mat-form-field fxFlex class="mat-block" *ngIf="!hideSelectType">
          <mat-label translate>notification.type</mat-label>
          <mat-select formControlName="notificationType">
            <mat-option *ngFor="let notificationType of notificationTypes" [value]="notificationType">
              {{ notificationTemplateTypeTranslateMap.get(notificationType).name | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <section formGroupName="configuration">
          <label [ngClass]="{'tb-error': templateNotificationForm.get('configuration.deliveryMethodsTemplates').hasError('atLeastOne')}"
            class="tb-title tb-required">{{ "notification.delivery-methods" | translate }}</label>
          <div class="tb-hint" translate>notification.at-least-one-should-be-selected</div>
          <section formGroupName="deliveryMethodsTemplates" class="delivery-methods-container">
            <section *ngFor="let deliveryMethods of notificationDeliveryMethods"
                     class="delivery-method-container"
                     [formGroupName]="deliveryMethods">
              <mat-slide-toggle class="delivery-method" formControlName="enabled">
                {{ notificationDeliveryMethodTranslateMap.get(deliveryMethods) | translate }}
              </mat-slide-toggle>
            </section>
          </section>
        </section>
      </form>
    </mat-step>
    <mat-step *ngIf="templateNotificationForm.get('configuration.deliveryMethodsTemplates.WEB.enabled').value"
      [stepControl]="webTemplateForm">
      <ng-template matStepLabel>{{ 'notification.delivery-method.web' | translate }}</ng-template>
      <div class="tb-hint-available-params mat-body-2">
        <span class="content">{{ 'notification.input-fields-support-templatization' | translate}}</span>
        <span tb-help-popup="{{ notificationTemplateTypeTranslateMap.get(templateNotificationForm.get('notificationType').value).helpId }}"
              tb-help-popup-placement="bottom"
              trigger-style="letter-spacing:0.25px"
              [tb-help-popup-style]="{maxWidth: '800px'}"
              trigger-text="{{ 'notification.see-documentation' | translate }}"></span>
      </div>
      <form [formGroup]="webTemplateForm">
        <mat-form-field class="mat-block">
          <mat-label translate>notification.subject</mat-label>
          <input matInput formControlName="subject">
          <mat-error *ngIf="webTemplateForm.get('subject').hasError('required')">
            {{ 'notification.subject-required' | translate }}
          </mat-error>
        </mat-form-field>
        <mat-form-field class="mat-block">
          <mat-label translate>notification.message</mat-label>
          <textarea matInput
                    cdkTextareaAutosize
                    cols="1"
                    cdkAutosizeMinRows="1"
                    formControlName="body">
          </textarea>
          <mat-error *ngIf="webTemplateForm.get('body').hasError('required')">
            {{ 'notification.message-required' | translate }}
          </mat-error>
        </mat-form-field>
        <section formGroupName="additionalConfig">
          <section formGroupName="icon" class="additional-config-group">
            <mat-slide-toggle formControlName="enabled" class="toggle">
              {{ 'icon.icon' | translate }}
            </mat-slide-toggle>
            <div *ngIf="webTemplateForm.get('additionalConfig.icon.enabled').value"
                 fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayout.xs="column">
              <tb-material-icon-select formControlName="icon" required fxFlex>
              </tb-material-icon-select>
              <tb-color-input formControlName="color" fxFlex>
              </tb-color-input>
            </div>
          </section>
          <section formGroupName="actionButtonConfig" class="additional-config-group">
            <mat-slide-toggle formControlName="enabled" class="toggle">
              {{ 'notification.action-button' | translate }}
            </mat-slide-toggle>
            <div *ngIf="webTemplateForm.get('additionalConfig.actionButtonConfig.enabled').value">
              <div fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayout.xs="column">
                <mat-form-field class="mat-block" fxFlex>
                  <mat-label translate>notification.button-text</mat-label>
                  <input matInput formControlName="text" required>
                  <mat-error *ngIf="webTemplateForm.get('additionalConfig.actionButtonConfig.text').hasError('required')">
                    {{ 'notification.button-text-required' | translate }}
                  </mat-error>
                  <mat-error *ngIf="webTemplateForm.get('additionalConfig.actionButtonConfig.text').hasError('maxlength')">
                    {{ 'notification.button-text-max-length' | translate :
                    {length: webTemplateForm.get('additionalConfig.actionButtonConfig.text').getError('maxlength').requiredLength}
                    }}
                  </mat-error>
                </mat-form-field>
              </div>
              <div fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayout.xs="column">
                <mat-form-field fxFlex="30" fxFlex.xs="100">
                  <mat-label translate>notification.action-type</mat-label>
                  <mat-select formControlName="linkType">
                    <mat-option *ngFor="let actionButtonLinkType of actionButtonLinkTypes" [value]="actionButtonLinkType">
                      {{ actionButtonLinkTypeTranslateMap.get(actionButtonLinkType) | translate }}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
                <mat-form-field fxFlex
                  *ngIf="webTemplateForm.get('additionalConfig.actionButtonConfig.linkType').value === actionButtonLinkType.LINK; else dashboardSelector">
                  <mat-label translate>notification.link</mat-label>
                  <input matInput formControlName="link" required>
                  <mat-error *ngIf="webTemplateForm.get('additionalConfig.actionButtonConfig.link').hasError('required')">
                    {{ 'notification.link-required' | translate }}
                  </mat-error>
                </mat-form-field>
                <ng-template #dashboardSelector>
                  <tb-dashboard-autocomplete
                    fxFlex="35" fxFlex.xs="100"
                    required
                    formControlName="dashboardId">
                  </tb-dashboard-autocomplete>
                  <tb-dashboard-state-autocomplete fxFlex="35" fxFlex.xs="100"
                    [dashboardId]="webTemplateForm.get('additionalConfig.actionButtonConfig.dashboardId').value"
                    formControlName="dashboardState">
                  </tb-dashboard-state-autocomplete>
                </ng-template>
              </div>
              <mat-slide-toggle formControlName="setEntityIdInState" class="toggle"
                                *ngIf="webTemplateForm.get('additionalConfig.actionButtonConfig.linkType').value === actionButtonLinkType.DASHBOARD">
                {{ 'notification.set-entity-from-notification' | translate }}
              </mat-slide-toggle>
            </div>
          </section>
        </section>
      </form>
    </mat-step>
    <mat-step *ngIf="templateNotificationForm.get('configuration.deliveryMethodsTemplates.EMAIL.enabled').value"
      [stepControl]="emailTemplateForm" #emailStep="matStep">
      <ng-template matStepLabel>{{ 'notification.delivery-method.email' | translate }}</ng-template>
      <ng-template matStepContent>
        <div class="tb-hint-available-params mat-body-2">
          <span class="content">{{ 'notification.input-fields-support-templatization' | translate}}</span>
          <span tb-help-popup="{{ notificationTemplateTypeTranslateMap.get(templateNotificationForm.get('notificationType').value).helpId }}"
                tb-help-popup-placement="bottom"
                trigger-style="letter-spacing:0.25px"
                [tb-help-popup-style]="{maxWidth: '800px'}"
                trigger-text="{{ 'notification.see-documentation' | translate }}"></span>
        </div>
        <form [formGroup]="emailTemplateForm">
          <mat-form-field class="mat-block">
            <mat-label translate>notification.subject</mat-label>
            <input matInput formControlName="subject">
            <mat-error *ngIf="emailTemplateForm.get('subject').hasError('required')">
              {{ 'notification.subject-required' | translate }}
            </mat-error>
          </mat-form-field>
          <mat-label class="tb-title tb-required"
                     [ngClass]="{'tb-error': (emailStep.interacted || emailTemplateForm.get('body').dirty) && emailTemplateForm.get('body').hasError('required')}"
                     translate>notification.message</mat-label>
          <editor [init]="tinyMceOptions" formControlName="body"></editor>
          <mat-error class="tb-mat-error"
            *ngIf="(emailStep.interacted || emailTemplateForm.get('body').dirty) && emailTemplateForm.get('body').hasError('required')">
            {{ 'notification.message-required' | translate }}
          </mat-error>
        </form>
      </ng-template>
    </mat-step>
    <mat-step *ngIf="templateNotificationForm.get('configuration.deliveryMethodsTemplates.SMS.enabled').value"
              [stepControl]="smsTemplateForm">
      <ng-template matStepLabel>{{ 'notification.delivery-method.sms' | translate }}</ng-template>
      <div class="tb-hint-available-params mat-body-2">
        <span class="content">{{ 'notification.input-field-support-templatization' | translate}}</span>
        <span tb-help-popup="{{ notificationTemplateTypeTranslateMap.get(templateNotificationForm.get('notificationType').value).helpId }}"
              tb-help-popup-placement="bottom"
              trigger-style="letter-spacing:0.25px"
              [tb-help-popup-style]="{maxWidth: '800px'}"
              trigger-text="{{ 'notification.see-documentation' | translate }}"></span>
      </div>
      <form [formGroup]="smsTemplateForm">
        <mat-form-field class="mat-block" subscriptSizing="dynamic">
          <mat-label translate>notification.message</mat-label>
          <textarea matInput
                    cdkTextareaAutosize
                    cols="1"
                    cdkAutosizeMinRows="1"
                    formControlName="body">
          </textarea>
          <mat-error *ngIf="smsTemplateForm.get('body').hasError('required')">
            {{ 'notification.message-required' | translate }}
          </mat-error>
          <mat-error *ngIf="smsTemplateForm.get('body').hasError('maxlength')">
            {{ 'notification.message-max-length' | translate :
            {length: smsTemplateForm.get('body').getError('maxlength').requiredLength}
            }}
          </mat-error>
        </mat-form-field>
      </form>
    </mat-step>
    <mat-step *ngIf="templateNotificationForm.get('configuration.deliveryMethodsTemplates.SLACK.enabled').value"
              [stepControl]="slackTemplateForm">
      <ng-template matStepLabel>{{ 'notification.delivery-method.slack' | translate }}</ng-template>
      <div class="tb-hint-available-params mat-body-2">
        <span class="content">{{ 'notification.input-field-support-templatization' | translate}}</span>
        <span tb-help-popup="{{ notificationTemplateTypeTranslateMap.get(templateNotificationForm.get('notificationType').value).helpId }}"
              tb-help-popup-placement="bottom"
              trigger-style="letter-spacing:0.25px"
              [tb-help-popup-style]="{maxWidth: '800px'}"
              trigger-text="{{ 'notification.see-documentation' | translate }}"></span>
      </div>
      <form [formGroup]="slackTemplateForm" fxLayoutGap="8px">
        <mat-form-field class="mat-block">
          <mat-label translate>notification.message</mat-label>
          <textarea matInput
                    cdkTextareaAutosize
                    cols="1"
                    cdkAutosizeMinRows="1"
                    formControlName="body">
          </textarea>
          <mat-error *ngIf="slackTemplateForm.get('body').hasError('required')">
            {{ 'notification.message-required' | translate }}
          </mat-error>
        </mat-form-field>
      </form>
    </mat-step>
  </mat-horizontal-stepper>
</div>
<mat-divider></mat-divider>
<div mat-dialog-actions fxLayout="row">
  <button mat-stroked-button *ngIf="selectedIndex > 0"
          (click)="backStep()">{{ 'action.back' | translate }}</button>
  <span fxFlex></span>
  <button mat-raised-button
          color="primary"
          (click)="nextStep()">{{ nextStepLabel() | translate }}</button>
</div>
